unit UsuarioRecordFr;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  StdCtrls, ComCtrls, DBCtrls, ActnList, Buttons, DBGrids, MainRecordFr, md5;

type

  { TUsuarioRecordFrame }

  TUsuarioRecordFrame = class(TMainRecordFrame)
    DBLookupComboBox1: TDBLookupComboBox;
    Label5: TLabel;
    DBGrid1: TDBGrid;
    GroupBox2: TGroupBox;
    InserirPerfilAction: TAction;
    SalvarPerfilAction: TAction;
    CancelarPerfilAction: TAction;
    SenhaEdit: TEdit;
    BitBtn1: TBitBtn;
    IDUsuarioDBEdit: TDBEdit;
    IDPessoaDBEdit: TDBEdit;
    nome_pessoa: TDBEdit;
    DBEdit4: TDBEdit;
    AtivoDBRadio: TDBRadioGroup;
    GroupBox1: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    procedure BitBtn1Click(Sender: TObject);
    procedure IDPessoaDBEditChange(Sender: TObject);
    procedure InserirActionExecute(Sender: TObject);
    procedure SalvarActionExecute(Sender: TObject);
    procedure ControlaBotoes(); override;


  private
    { private declarations }
  public
    { public declarations }
  end;

implementation

uses
  Initializator, MainFr, UsuarioDM, MainDM;

{ TUsuarioRecordFrame }

procedure TUsuarioRecordFrame.ControlaBotoes();
var
  I: integer;
begin
  SenhaEdit.Text:='';
  // Desabilita todas as ações
  for I := 0 to RecordActionList.ActionCount - 1 do begin
    TAction(RecordActionList.Actions[I]).Enabled := False;
  end;
  // Habilita apenas localizar e fechar
  LocalizarAction.Enabled := True;
  FecharAction.Enabled := True;
  // Verifica se o frame possui módulo de dados
  if (UsuarioData.DataSetState() = mdsDisable) then begin
    // Se não tiver desabilita todos os componentes
    for I := 0 to Self.ComponentCount - 1 do
      if Self.Components[I] is TControl then
        TControl(Self.Components[I]).Enabled := False;
  end else begin
    // Verifica se o DataSet está em modo de edição e controla habilitação das ações
    if (UsuarioData.DataSetState() in [mdsEditing, mdsInserting]) then begin
      SalvarAction.Enabled := True;
      CancelarAction.Enabled := True;
    end else begin
      InserirAction.Enabled := True;
      if (UsuarioData.DataSetState() = mdsCanEdit) then begin
        ExcluirAction.Enabled := True;
        AlterarAction.Enabled := True;
      end;
    end;
  end;
end;

procedure TUsuarioRecordFrame.IDPessoaDBEditChange(Sender: TObject);
begin
  if (UsuarioData.DataSetState() in [mdsEditing, mdsInserting]) then begin
    UsuarioData.UsuarioZQuery.FieldByName('id_usuario').AsString := IDPessoaDBEdit.Text;
  end;
end;

procedure TUsuarioRecordFrame.InserirActionExecute(Sender: TObject);
begin
  UsuarioData.BuscaUsuarios();
  inherited;
end;

procedure TUsuarioRecordFrame.SalvarActionExecute(Sender: TObject);
var
  Senha: string;
begin
  Senha := MD5Print(MD5String(SenhaEdit.Text));
  UsuarioData.InsereValores(Senha);
  UsuarioData.BuscaUsuarios();
  ControlaBotoes();
end;

procedure TUsuarioRecordFrame.BitBtn1Click(Sender: TObject);
begin
  AtivoDBRadio.ItemIndex := 0;
  TheControl.ShowForeignFrame(TMainFrame(PessoaSearchFrame));
end;


initialization
  {$I usuariorecordfr.lrs}

end.

